/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.impl;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.service.scene.InitSceneOfferInstService;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdHisAttrCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.inst.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneFuncProdInstService;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneOfferInstService;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneProdInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdFuncVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.log.KiteLogEnum;
import com.iwhalecloud.bss.kite.common.log.KiteLogger;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosProductDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.AddRoleProd;
import com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.IAddRoleProdService;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.cucc.service.util.OfferDealUtils;
import com.iwhalecloud.bss.kite.cucc.spec.impl.transfer.GoodsInfoTransfer;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Product;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.dto.KiteInstRelDTO;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.scene.service.operate.OperateChangeService;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * <Description> <br>
 *
 *
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月27日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.impl <br>
 */
@AddRoleProd(value= {"2000880","2101113","2101115","2101106","2101108","2101111"})
@Service
public class AddRoleProdServiceImpl implements IAddRoleProdService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneOfferInstService operateSceneOfferInstService;

    @Autowired
    private IOperateSceneProdInstService operateSceneProdInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Autowired
    private IOperateSceneFuncProdInstService operateSceneFuncProdInstService;

    @Resource(name = "manager.operateChangeService")
    private OperateChangeService operateChangeService;

    @Autowired
    private InitSceneOfferInstService initSceneOfferInstService;
    /**
     * Description: <br>
     * @author tengg<br>
     * @taskId <br>
     * @param sceneInstId
     * @param custId
     * @param staffId
     * @param offerInsts <br>
     */
    @Override
    @KiteTransactional
    @KiteLogger(isWrite = true, type = KiteLogEnum.SERVICE)
    public void initAddRoleOfferInst(String sceneInstId, String custId, String staffId,
        List<KitePosGoodsDTO> offerInsts) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        if (KiteListUtils.isEmpty(offerInsts)) {
            return;
        }
        List<KitePosGoodsDTO> addRoleOfferInsts = offerInsts.stream().filter(offerInst -> GoodsInfoTransfer.isAddRoleOffer(offerInst.getCode(), offerInst.getCatagoryId())).collect(Collectors.toList());
        if (KiteListUtils.isEmpty(addRoleOfferInsts)) {
            return;
        }
        List<String> addRoleInstanceId = new ArrayList<>();
        for (KitePosGoodsDTO offerInst : addRoleOfferInsts) {
            String parentInstId = null;
            String parentOfferId = null;
            String parentOfferName = null;
            // 获取附加商品的主商品
            List<KitePosGoodsDTO> parentOfferInsts = offerInsts.stream().filter(offerInst1 -> KiteStringUtils.equals(offerInst.getParentInstId(), offerInst1.getInstanceId())).collect(Collectors.toList());
            if (!KiteListUtils.isEmpty(parentOfferInsts)) {
                // 主商品的商品ID
                String mainOfferId = parentOfferInsts.get(0).getCode();
                // 主商品实例
                List<KiteOfferInst> mainOfferInsts = querySceneInstService.listMainOfferInsts(sceneInstId, mainOfferId);
                if (!KiteListUtils.isEmpty(mainOfferInsts)) {
                    parentInstId = mainOfferInsts.get(0).getOfferInstId();
                    parentOfferId = mainOfferInsts.get(0).getAttrValue("saleGoodsCode");
                    parentOfferName = mainOfferInsts.get(0).getAttrValue("saleGoodsName");
                }
            }

            String offerId = offerInst.getCode();
            List<KiteOfferInst> addRoleInsts = this.querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.MAIN_OFFER_INST), offerId);
            String additionalOfferInstId = null;
            if (KiteListUtils.isNotEmpty(addRoleInsts) ) {
                for (KiteOfferInst addRoleInst: addRoleInsts) {
                    if (addRoleInstanceId.contains(addRoleInst.getOfferInstId())) {
                        continue;
                    }
                    additionalOfferInstId = addRoleInst.getOfferInstId();
                }
            }
            // 附属销售品实例
            KiteOfferInst kiteOfferInst = null;
            if (KiteStringUtils.isEmpty(additionalOfferInstId)){
                //没有实例化，需要进行实例化再设置属性
                kiteOfferInst = operateSceneOfferInstService.addOfferInst(sceneInstId, parentInstId, offerId, offerInst.getCatagoryId(), custId, staffId).getResultObject();
            }
            else {
                kiteOfferInst = querySceneInstService.getOfferInst(custId, additionalOfferInstId);
            }
            addRoleInstanceId.add(kiteOfferInst.getOfferInstId());
            CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "saleGoodsCode", parentOfferId);
            CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "saleGoodsName", parentOfferName);
            CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "startDate", offerInst.getStartDate());
            CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "endDate", offerInst.getEndDate());
            String destItemId = offerInst.getDestItemId();
            if (KiteStringUtils.isNotEmpty(destItemId)) {
                kiteOfferInst.setExtOfferInstId(destItemId);
                kiteOfferInst.setActionType(ActionType.K);
                operateSceneInstService.addMainOfferInst(sceneInstId, kiteOfferInst);
                operateChangeService.setDeleteInstChange(sceneInst, kiteOfferInst, false, null, "");
            }
            List<KitePosProductDTO> products = offerInst.getProducts();
            for (KitePosProductDTO product : products) {
                List<KiteInstRelDTO> instRels = querySceneInstService.listInstRelsByAInst(custId, InstType.MAIN_OFFER_INST, kiteOfferInst.getOfferInstId(), InstType.PROD_INST);
                String prodInstId = instRels.get(0).getzInstId();
                KiteProdInst prodInst = querySceneInstService.getProdInst(custId, prodInstId);

                // 非属性组属性
                List<KitePosAttrDTO> kiteAttrs = product.getAttrs().stream().filter(attr -> !KiteStringUtils.isEqual("ATTR_GROUP", attr.getType())).collect(Collectors.toList());
                //安全产品基础业务属性转换为功能产品展示
                Map<String,List<KitePosAttrDTO>> baseFunckiteAttrMap = new HashMap<>();
                List<DcPublic> spDcs = DcPublicCache.getByPkey("2021102107", offerId);
                if(KiteListUtils.isNotEmpty(spDcs)){
                    for(DcPublic spdc:spDcs){
                        String [] funMainAttr = spDcs.get(0).getPname().split(",");
                        List<KitePosAttrDTO> baseFunckiteAttrs = kiteAttrs.stream().filter(ktIt -> KiteStringUtils.equalsAny(ktIt.getCode(), funMainAttr)).collect(Collectors.toList());
                        baseFunckiteAttrMap.put(spdc.getPcode(),baseFunckiteAttrs);
                        kiteAttrs.removeIf(ktIt->KiteStringUtils.equalsAny(ktIt.getCode(),funMainAttr));
                    }
                }
                OfferDealUtils.setProdInstAttrValue(sceneInstId, prodInst, kiteAttrs);
                this.setHisProdInstAttrValue(sceneInst, prodInst, prodInst);
                // 属性组属性
                List<KitePosAttrDTO> attrGroups = product.getAttrs().stream().filter(attr -> KiteStringUtils.isEqual("ATTR_GROUP", attr.getType())).collect(Collectors.toList());

                // 获取功能产品属性
                List<KiteAttr> connectButtonTypeAttrs = prodInst.listAttrs().stream().filter(attr -> KiteStringUtils.equals(KeyConsts.GEN_LOGIC, attr.getAttrValueTypeId())).collect(Collectors.toList());
                List<KiteAttr> prodFuncIdAttrs = connectButtonTypeAttrs.stream().filter(idAttr -> KiteStringUtils.equals(ProductCache.get(idAttr.getGroupId()).getProdFuncType(), KeyConsts.PROD_FUNC_TYPE_102)).collect(Collectors.toList());
                //过滤附件属性(大数据附件还原成附件属性)
                List<KitePosAttrDTO> fjGroupAttr = attrGroups.stream().filter(groupAttr -> KiteListUtils.isNotEmpty(DcPublicCache.getByPkey("20210506", groupAttr.getCode()))).collect(Collectors.toList());
                attrGroups.removeIf(groupAttr -> KiteListUtils.isNotEmpty(DcPublicCache.getByPkey("20210506", groupAttr.getCode())));
                initSceneOfferInstService.initAttachmentFromAttrGrp(prodInst,fjGroupAttr,offerId,sceneInstId);
                // 过滤不属于功能产品的属性组
                List<KitePosAttrDTO> prodAttrGroups =  attrGroups.stream().filter(groupAttr ->{
                    for (KiteAttr prodFunc : prodFuncIdAttrs) {
                        if (KiteStringUtils.isEqual(ProductCache.get(prodFunc.getGroupId()).getProdNbr(), groupAttr.getCode())) {
                            return false;
                        }
                    }
                    return true;
                }).collect(Collectors.toList());
                //将非功能产品属性组的值，还原至主产品实例属性
                prodAttrGroups.forEach(prodAttrGroup ->{
                 // 后面转单组装属性组时用到
                    Map<String, KitePosAttrDTO> orgAttrGroupMap = querySceneInstService
                        .getExtParameterValue(sceneInst.getSceneInstId(), "attrGroupMap_".concat(prodInstId));
                    if (Objects.nonNull(orgAttrGroupMap)) {
                        orgAttrGroupMap.put(prodAttrGroup.getCode(), prodAttrGroup);
                    }
                    else {
                        Map<String, KitePosAttrDTO> attrGroupMap = new HashMap<>();
                        attrGroupMap.put(prodAttrGroup.getCode(), prodAttrGroup);
                        operateSceneInstService
                            .putExtParameter(sceneInst.getSceneInstId(), "attrGroupMap_".concat(prodInstId), attrGroupMap);
                    }
                    String attrValue = prodAttrGroup.getValue();
                    Map<String, Object> attrValueMap = JSONObject.parseObject(attrValue, Map.class);
                    Iterator<String> it = attrValueMap.keySet().iterator();
                    while (it.hasNext()) {
                        String attrGroupInstId = it.next();
                        JSONArray attrListValue = (JSONArray) attrValueMap.get(attrGroupInstId);
                        //prodInst.setExtParamValue(LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + prodAttrGroup.getCode(), attrGroupInstId);
                        CuccPosServiceUtils.setExtParamValue(prodInst, sceneInstId, LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + prodAttrGroup.getCode(), attrGroupInstId);
                        List<KitePosAttrDTO> prodInstAttrs = JSONArray.parseArray(JSONArray.toJSONString(attrListValue), KitePosAttrDTO.class);
                        OfferDealUtils.setProdInstAttrValue(sceneInst.getSceneInstId(), prodInst, prodInstAttrs);
                        this.setHisProdInstAttrValue(sceneInst, prodInst, prodInst);
                    }
                });
                // 将功能属性组设置为功能产品
                initAddRoleFuncProduct (sceneInst, custId, staffId, prodInst, attrGroups, prodFuncIdAttrs);
                //安全产品将业务信息还原为功能产品
                if(KiteMapUtils.isNotEmpty(baseFunckiteAttrMap)){
                    initAddRoleBaseAttrFuncProduct(sceneInst,custId,staffId,prodInst,baseFunckiteAttrMap);
                }
             // 还原外系统实例Id
                String prodDestItemId = product.getDestItemId();
                if (KiteStringUtils.isNotEmpty(prodDestItemId)) {
                    prodInst.setExtProdInstId(prodDestItemId);
                    prodInst.setActionType(ActionType.K);
                    operateSceneInstService.addProdInst(sceneInstId, prodInst);
                    operateChangeService.setDeleteInstChange(sceneInst, prodInst, false, null, "");
                }
                // 还原actionType
                String changeType = product.getChangeType();
                if (KiteStringUtils.isNotEmpty(changeType)) {
                    if (KiteStringUtils.equals(ChangeTypeUtils.CHANGE_TYPE_D, changeType)) {
                        operateSceneProdInstService.removeProdInst(sceneInstId, prodInstId);
                    }
                    else {
                        prodInst.setActionType(ChangeTypeUtils.getActionType(changeType));
                        operateSceneInstService.addProdInst(sceneInstId, prodInst);
                    }
                }

            }

            // 还原销售品实例动作
            String offerChangeType = offerInst.getChangeType();
            if (KiteStringUtils.isNotEmpty(offerChangeType)) {
                if (KiteStringUtils.equals(ChangeTypeUtils.CHANGE_TYPE_D, offerChangeType)) {
                    String offerType = OfferCache.get(kiteOfferInst.getOfferId()).getOfferType();
                    operateSceneOfferInstService.removeOffer(sceneInstId, custId, kiteOfferInst.getOfferInstId(), offerType);
                }
                else {
                    kiteOfferInst.setActionType(ChangeTypeUtils.getActionType(offerChangeType));
                    operateSceneInstService.addMainOfferInst(sceneInstId, kiteOfferInst);
                }
            }
        }
    }

    private void initAddRoleFuncProduct(KiteSceneInst sceneInst, String custId, String staffId, KiteProdInst prodInst, List<KitePosAttrDTO> attrGroups, List<KiteAttr> prodFuncIdAttrs) {
     // 如果附加产品没有属性组，只实例化一个附加产品，如果有属性组，根据属性组数量实例化并返填属性值
        if (KiteListUtils.isEmpty(attrGroups) || KiteListUtils.isEmpty(prodFuncIdAttrs)) {
            return;
        }
        String prodInstId = prodInst.getProdInstId();
        //98H的属性的组ID，为功能产品ID
        for (KiteAttr prodFuncAttr : prodFuncIdAttrs) {
            String groupId = prodFuncAttr.getGroupId();
            Product funcProdSpec = ProductCache.get(groupId);
            if (Objects.isNull(funcProdSpec)) {
                continue;
            }
            // 一个功能产品只能会有一个属性组
            KitePosAttrDTO kitePosAttrGroup = attrGroups.stream().filter(attrGroup -> KiteStringUtils.equals(funcProdSpec.getProdNbr(), attrGroup.getCode())).findFirst().orElse(null);
            // 如果当前功能产品没有属性组，那么就不转换为功能产品实例
            if (Objects.isNull(kitePosAttrGroup)) {
                continue;
            }
            // 后面转单组装属性组时用到
            Map<String, KitePosAttrDTO> orgAttrGroupMap = querySceneInstService
                .getExtParameterValue(sceneInst.getSceneInstId(), "attrGroupMap_".concat(prodInstId));
            if (Objects.nonNull(orgAttrGroupMap)) {
                orgAttrGroupMap.put(kitePosAttrGroup.getCode(), kitePosAttrGroup);
            }
            else {
                Map<String, KitePosAttrDTO> attrGroupMap = new HashMap<>();
                attrGroupMap.put(kitePosAttrGroup.getCode(), kitePosAttrGroup);
                operateSceneInstService
                    .putExtParameter(sceneInst.getSceneInstId(), "attrGroupMap_".concat(prodInstId), attrGroupMap);
            }
            String attrValue = kitePosAttrGroup.getValue();
            Map<String, Object> attrValueMap = JSONObject.parseObject(attrValue, Map.class);
            Iterator<String> it = attrValueMap.keySet().iterator();
            while (it.hasNext()) {
                String attrGroupInstId = it.next();
                JSONArray attrListValue = (JSONArray) attrValueMap.get(attrGroupInstId);
                if(Objects.isNull(attrListValue)||attrListValue.isEmpty()){
                    //全删除后仍返回实例 不还原
                    continue;
                }
                DataResult<ProdFuncVO> result = operateSceneFuncProdInstService.addFuncProdInst(sceneInst.getSceneInstId(), prodInstId, groupId, custId, staffId);
                KiteProdInstFunc prodInstFunc = this.querySceneInstService.getProdInstFunc(custId, result.getResultObject().getProdInstId());
                //prodInstFunc.setExtParamValue(LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + funcProdSpec.getProdNbr(), attrGroupInstId);
                CuccPosServiceUtils.setExtParamValue(prodInstFunc, sceneInst.getSceneInstId(), LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + funcProdSpec.getProdNbr(), attrGroupInstId);

                List<KitePosAttrDTO> prodInstAttrs = JSONArray.parseArray(JSONArray.toJSONString(attrListValue), KitePosAttrDTO.class);
                OfferDealUtils.setProdInstAttrValue(sceneInst.getSceneInstId(), prodInstFunc, prodInstAttrs);
                this.setHisProdInstAttrValue(sceneInst, prodInst, prodInstFunc);
                prodInstFunc.setActionType(ChangeTypeUtils.getActionType(ChangeTypeUtils.CHANGE_TYPE_K));
                operateSceneInstService.addProdInstFunc(sceneInst.getSceneInstId(), prodInstFunc);
                String changeType = OfferDealUtils.dealFunProdInstActionType(prodInstAttrs);
                if(!KiteStringUtils.equals(ChangeTypeUtils.CHANGE_TYPE_A,changeType)){
                    operateChangeService.setDeleteInstChange(sceneInst, prodInstFunc, false, null, "");
                }
                if (KiteStringUtils.isNotEmpty(changeType)) {
                    // 还原他的动作
                    if (KiteStringUtils.equals(ChangeTypeUtils.CHANGE_TYPE_D, changeType)) {
                        operateSceneFuncProdInstService.removeFuncProdInst(sceneInst.getSceneInstId(), prodInstId, prodInstFunc.getProdInstId(), custId, staffId);
                    }
                    else {
                        prodInstFunc.setActionType(ChangeTypeUtils.getActionType(changeType));
                        operateSceneInstService.addProdInstFunc(sceneInst.getSceneInstId(), prodInstFunc);
                    }
                }

            }
        }
    }
    private void initAddRoleBaseAttrFuncProduct(KiteSceneInst sceneInst, String custId, String staffId, KiteProdInst prodInst, Map<String, List<KitePosAttrDTO>> baseFunckiteAttrMap) {

        Set<Map.Entry<String, List<KitePosAttrDTO>>> baseFuncEns = baseFunckiteAttrMap.entrySet();
        String prodInstId = prodInst.getProdInstId();
        //98H的属性的组ID，为功能产品ID
        for (Map.Entry<String, List<KitePosAttrDTO>> baseFuncEn : baseFuncEns) {
            String groupId = baseFuncEn.getKey();
            Product funcProdSpec = ProductCache.get(groupId);
            if (Objects.isNull(funcProdSpec)) {
                continue;
            }
            // 后面转单组装属性组时用到
            Map<String, KitePosAttrDTO> orgAttrGroupMap = querySceneInstService
                .getExtParameterValue(sceneInst.getSceneInstId(), "attrGroupMap_".concat(prodInstId));
            DataResult<ProdFuncVO> result = operateSceneFuncProdInstService.addFuncProdInst(sceneInst.getSceneInstId(), prodInstId, groupId, custId, staffId);
            KiteProdInstFunc prodInstFunc = this.querySceneInstService.getProdInstFunc(custId, result.getResultObject().getProdInstId());
            //CuccPosServiceUtils.setExtParamValue(prodInstFunc, sceneInst.getSceneInstId(), LocalKeyConsts.PREFIX_ATTR_GROUP_INST_ID + funcProdSpec.getProdNbr(), attrGroupInstId);
            List<KitePosAttrDTO> prodInstAttrs = baseFuncEn.getValue();
            OfferDealUtils.setProdInstAttrValue(sceneInst.getSceneInstId(), prodInstFunc, prodInstAttrs);
            this.setHisProdInstAttrValue(sceneInst, prodInst, prodInstFunc);
            if(KiteStringUtils.equals(groupId,"202102177023")&&KiteStringUtils.equals("110007147",prodInstFunc.getAttrValue("210004110"))){//安全产品E套餐判断
                operateSceneInstService.putExtParameter(sceneInst.getSceneInstId(), "SAFE_KD_ISE", "T");
            }
            prodInstFunc.setActionType(ChangeTypeUtils.getActionType(ChangeTypeUtils.CHANGE_TYPE_K));
            operateSceneInstService.addProdInstFunc(sceneInst.getSceneInstId(), prodInstFunc);
            operateChangeService.setDeleteInstChange(sceneInst, prodInstFunc, false, null, "");
            String changeType = OfferDealUtils.dealFunProdInstActionType(prodInstAttrs);
            if (KiteStringUtils.isNotEmpty(changeType)) {
                // 还原他的动作
                if (KiteStringUtils.equals(ChangeTypeUtils.CHANGE_TYPE_D, changeType)) {
                    operateSceneFuncProdInstService.removeFuncProdInst(sceneInst.getSceneInstId(), prodInstId, prodInstFunc.getProdInstId(), custId, staffId);
                }
                else {
                    prodInstFunc.setActionType(ChangeTypeUtils.getActionType(changeType));
                    operateSceneInstService.addProdInstFunc(sceneInst.getSceneInstId(), prodInstFunc);
                }
            }

            }
        }

    private void setHisProdInstAttrValue(KiteSceneInst sceneInst, KiteProdInst accProdInst, KiteInst prodInst) {
        if(KiteObjectUtils.isEmpty(accProdInst) || KiteObjectUtils.isEmpty(prodInst)){
            return;
        }
        String prodId = accProdInst.getSpecId();
        String offerId = null;
        List<KiteOfferInst> kiteOfferInsts = querySceneInstService.listMainOfferInstsByProdInstId(sceneInst.getCustId(), accProdInst.getProdInstId());
        if(KiteListUtils.isNotEmpty(kiteOfferInsts)){
            offerId = Optional.ofNullable(kiteOfferInsts.get(0)).map(KiteOfferInst::getSpecId).orElse(null);
        }
        if (!KiteStringUtils.isAnyEmpty(offerId, prodId)) {
            List<KiteAttr> hisAttrs = ProdHisAttrCache.getByOfferId(prodId, offerId);
            if (KiteListUtils.isNotEmpty(hisAttrs)) {
                hisAttrs.forEach(hisAttr -> {
                    KiteAttr attr = prodInst.getAttr(hisAttr.getFieldName());
                    if (Objects.nonNull(attr) && KiteStringUtils.isIndexOf(KeyConsts.LOAD_ATTR_VALUE_CTRL, attr.getAttrValueTypeId(), "/")
                        && KiteStringUtils.isNotEmpty(attr.getValue()) && KiteStringUtils.isEmpty(attr.getValueDesc())) {
                        List<KiteAttrValue> attrValues = attr.getAttrValues();
                        if (KiteListUtils.isNotEmpty(attrValues)) {
                            KiteAttrValue kiteAttrValueHis = hisAttr.getAttrValues().stream().filter(attrIt -> KiteStringUtils.equals(attrIt.getAttrValue(), attr.getValue())).findFirst().orElse(null);
                            if (Objects.nonNull(kiteAttrValueHis)) {
                                attrValues.add(kiteAttrValueHis);
                                attr.setAttrValues(attrValues);
                                attr.setValueDesc(kiteAttrValueHis.getAttrValueName());
                            }
                        }
                    }
                });
            }
        }
    }
}
